package com.expand.learn.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author zhanghaidong
 * @version v1.0
 * @description WebMvcConfigurerAdapter已过时
 * @date 2020/2/26
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private CustomPersonArgumentResolver customPersonArgumentResolver;
    @Autowired
    private CustomizeJsonReturnValueHandler customizeJsonReturnValueHandler;
    @Autowired
    private CustomizeHttpMessageConverter customizeHttpMessageConverter;

    @Bean
    public CustomizeInterceptor customizeInterceptor() {
        return new CustomizeInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(customizeInterceptor()).addPathPatterns("/**");
    }

    @Bean
    public CustomizePersonFormatter customizePersonFormatter() {
        return new CustomizePersonFormatter();
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(customizePersonFormatter());
    }


    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(customPersonArgumentResolver);
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        handlers.add(customizeJsonReturnValueHandler);
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(customizeHttpMessageConverter);
    }
}
